﻿using System.Collections.Generic;

namespace Config
{
    public static class ConfigParse
    {
        /// <summary>
        /// 读取配置表中字符串
        /// 换行符的转换
        /// </summary>
        public static string ReplaceNewLineSymbol(string str)
        {
            string result = str.Replace("\\n", "\n");
            result = result.Replace("/n", "\n");
            return result;
        }

        /// <summary>
        /// 判断配置表该项值是否为空
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool StringIsNull(string str)
        {
            return string.IsNullOrEmpty(str) || str == "0";
        }

        /// <summary>
        /// 删除产出txt的多余引号
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string DelQuotationMarks(string str)
        {
            if (str.IndexOf('\"') == 0)
            {
                str = str.Substring(1, str.Length - 2);
            }
            return str;
        }

        public static List<string> ToStringList(string str, char factor = ',')
        {
            if (StringIsNull(str))
            {
                return null;
            }
            List<string> res = new List<string>();
            string[] strs = str.Split(factor);
            res.AddRange(strs);
            if (string.IsNullOrEmpty(res[res.Count - 1]))
            {
                res.RemoveAt(res.Count - 1);
            }
            return res;
        }

        public static List<int> ToIntList(string str, char factor = ',')
        {
            List<int> result = new List<int>();
            List<string> strList = ToStringList(str, factor);
            if (strList != null && strList.Count > 0)
            {
                int Len = strList.Count;
                for (int i = 0; i < Len; i++)
                {
                    if (string.IsNullOrEmpty(strList[i]))
                        continue;
                    int data = int.Parse(strList[i]);
                    result.Add(data);
                }
            }
            return result;
        }

        public static List<uint> ToUintList(string str, char factor = ',')
        {
            List<uint> result = new List<uint>();
            List<string> strList = ToStringList(str, factor);
            if (strList != null && strList.Count > 0)
            {
                int Len = strList.Count;
                for (int i = 0; i < Len; i++)
                {
                    if (string.IsNullOrEmpty(strList[i]))
                        continue;
                    uint data = uint.Parse(strList[i]);
                    result.Add(data);
                }
            }
            return result;

        }
    }

}

